<style lang="scss" scoped>
.nav_section {
  width: 100%;
}

.topthumb {
  width: 100%;
  height: 195px;
}

.topthumb img {
	width: 100%;
	height: 195px;
}

.nav_section_items {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  padding: 15px;
  border-bottom: 1px solid #ddd;
  position: relative;
}

.nav_section_items:active {
  background: #ddd;
}

.nav_section_items .section_image {
  width: 125px;
  height: 80px;
}

.nav_section_items .section_image img {
  width: 100%;
  height: 100%;
}

.nav_section_items .section_cont {
  width: 200px;
  font-size: 13px;
  color: #a9a9a9;

}

.nav_section_items .section_cont div {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  display: block;
}

.nav_section_items .section_cont .section_cont_sub {
  font-size: 15px;
  line-height: 30px;
  color: #000;
  margin-bottom: 5px;
}

.nav_section_items .section_cont .section_cont_price {
  font-size: 16px;
  color: #d24a58;
}
</style>

<template>
  <div class="container hasNav">
    <van-nav-bar title="预约服务" left-arrow left-text="返回" @click-left="$router.back()" fixed />
    <div class="nav_section">
      <div class="topthumb">
        <img src="@/assets/img/env4.jpg" />
      </div>
      <div class="nav_section_items">
        <div class="section_image">
          <img :src="picUrl" />
        </div>
        <div class="section_cont">
          <div class="section_cont_sub">
            <span>{{ serviceName }}</span>
          </div>
          <div class="section_cont_price">价格：￥{{ linePrice }}</div>
          <div>{{ brief }}</div>
        </div>
      </div>
    </div>
    <div class="infos-box">
      <van-form @submit="onSubmit">
        <van-field readonly clickable name="calendar" :value="form.reservationDate" label="预约日期" placeholder="点击选择日期"
          @click="showCalendar = true" />
        <van-calendar v-model="showCalendar" :show-confirm="false" @confirm="onDateConfirm" />

        <van-field readonly clickable name="datetimePicker" :value="form.reservationTime" label="预约时间"
          placeholder="点击选择时间" @click="showPicker = true" />

        <van-popup v-model="showPicker" position="bottom">
          <van-datetime-picker type="time" @confirm="onTimeConfirm" @cancel="showPicker = false" />
        </van-popup>

        <van-field v-model="form.note" rows="3" autosize label="预约留言" type="textarea" maxlength="300" placeholder="请输入留言"
          show-word-limit />

        <div style="margin: 16px;">
          <van-button block type="default" color="#d24a58" native-type="submit">提交</van-button>
        </div>
      </van-form>
    </div>
    <Loading :isShow="loading"></Loading>
  </div>
</template>

<script>
import moment from 'moment';
import Loading from '@/components/Loading';
import { getServiceInfo } from '../../api/service';
import { fileUrl, saveReservation } from '../../api/reservation';

function isNull(val) {
  return val === '' || !val
}

export default {
  name: 'reservation',
  components: {
    Loading,
  },
  data() {
    return {
      fileApi: fileUrl,
      loading: true,
      showCalendar: false,
      showPicker: false,
      serviceName: '',
      brief: '',
      picUrl: '',
      linePrice: 0.00,
      form: {
        serviceId: null,
        reservationDate: null,
        reservationTime: null,
        note: null,
      },
    };
  },
  mounted() {
    let id = this.$route.query.id;
    this.form.serviceId = id;
    this.getDetail(id);
  },
  methods: {
    async getDetail(id) {
      try {
        this.loading = true;
        const { code, message, data } = await getServiceInfo({ serviceId: id });
        if (code == 200) {
          this.serviceName = data.serviceName;
          this.brief = data.brief;
          this.picUrl = fileUrl + data.picUrl;
          this.linePrice = data.linePrice;
        } else {
          this.$toast(message);
        }
        this.$nextTick(() => {
          this.loading = false;
        });
      } catch (error) {
        console.log(error);
        this.loading = false;
      }
    },

    onDateConfirm(date) {
      let selectTime = `${moment(date).format('YYYY-MM-DD')}`;
      this.form.reservationDate = selectTime;
      this.showCalendar = false;
    },

    onTimeConfirm(time) {
      this.form.reservationTime = time;
      this.showPicker = false;
    },

    async onSubmit() {
      let formData = new FormData();
      if (isNull(this.form.reservationDate) || isNull(this.form.reservationTime)) {
        this.$toast('必须填写日期和时间')
        return;
      }
      formData.append('serviceId', this.form.serviceId);
      formData.append('reservationDate', this.form.reservationDate);
      formData.append('reservationTime', this.form.reservationTime);
      formData.append('note', this.form.note);
      const { code, message } = await saveReservation(formData)
      if (code == 200) {
        this.$toast('预约成功')
        let id = this.form.serviceId;
        this.$router.push({
          path: '/service/detail',
          query: { id }
        });
      } else {
        this.$toast(message)
      }
    }
  }
};
</script>